//
//  RobbCoreRoute.swift
//  Pods
//
//  Created by eme on 2017/3/24.
//
//

import UIKit
import UtilCore
import URLNavigator
import RxSwift
import RxCocoa
import RickonCore
import ModelProtocol

///公共模块
public struct BrandonCoreRoute {
    static let disposeBag = DisposeBag()
    public static func initialize() {
        
        /// 认证用户信息
        Navigator.map("/authvalidation") { url, values in
            let isneedlogin = url.queryParameters["isneedlogin"]
            switch AppData.appState {
            /// 当前状态为游客状态
            case .tourists:
                _ = "login".openURL()
                return false
            /// 审核未认证的状态
            case let .notAudit(user):
                if isneedlogin == "1"{
                    if let glshop = user.glShop,glshop.spid.characters.count > 0{
                        self.showMsgAndAction(msg: "您还未提交认证资料，认证通过后才能购买商品！", btnText: "现在认证", action: "authinfo".getUrlStr(param: ["spid":glshop.spid]))
                    }
                    return false
                }else{
                    return true
                }
            ///表示审核中
            case .auditing:
                if isneedlogin == "1"{
                    self.showMsgAndAction(msg: "您的认证资料还在审核中，是否前往查看详情？", btnText: "立即查看", action: "certification")
                    return false
                }else{
                    return true
                }
            ///表示审核失败
            case .auditfailed:
                if isneedlogin == "1"{
                    self.showMsgAndAction(msg: "您的认证资料审核失败，是否前往查看详情？", btnText: "立即查看", action: "certification")
                    return false
                }else{
                    return true
                }
            ///已经审核通过
            case .audited:
                return true
            }
        }
        /// 直接通过当前用户状态判断需要跳转到哪
        Navigator.map("/authaction") { url, values in
            let isneedlogin = url.queryParameters["isneedlogin"]
            switch AppData.appState {
            /// 当前状态为游客状态
            case .tourists:
                _ = "login".openURL()
                return false
                
            /// 审核未认证的状态
            case let .notAudit(user):
                if isneedlogin == "0"{
                    return true
                }else{
                    if let glshop = user.glShop,glshop.spid.characters.count > 0{
                        _ = "authinfo".openURL(["spid":glshop.spid])
                    }
                    return false
                }
            ///表示审核中 ///表示审核失败
            case .auditing ,.auditfailed:
                if isneedlogin == "0"{
                    return true
                }else{
                    _ = "certification".openURL()
                    return false
                    
                }
            ///已经审核通过
            case .audited:
                return true
            }
        }
    }
    public static func showMsgAndAction(msg:String,btnText:String,action:String){
        let attributedString = NSAttributedString(string: msg, attributes: [
            NSFontAttributeName : UIFont(name: Theme.font.normal, size: 14)!, //your font here
            NSForegroundColorAttributeName : UIColor(hex: "494949")
            ])
        let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
        /// attributedTitle
        alertController.setValue(attributedString, forKey: "attributedMessage")
        let leftAction = UIAlertAction(title: "随后再说", style: .default, handler: nil)
        leftAction.setValue(UIColor(hex: "989898"), forKey: "titleTextColor")
        let rightAction = UIAlertAction(title: btnText, style: .default ){_ in
            _ = action.openURL()
        }
        rightAction.setValue(Theme.btn.normal, forKey: "titleTextColor")
        alertController.addAction(leftAction)
        alertController.addAction(rightAction)
        Navigator.present(alertController)
    }
    
}
